﻿function Build-Package([string] $configuration, [string]$projectPath, [string]$outputPath, [boolean]$bPackage)
{
	$timer = [System.Diagnostics.StopWatch]::StartNew()
	
	if($bPackage)
	{
		# Packaging WSP dans le répertoire par défaut
		#Write-Host "`tPackaging ..." 
		. C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe  `
			/p:Configuration=$configuration `
			/m $projectPath `
			/t:Package `
			/p:UseWPP_CopyWebApplication=True `
			/p:PipelineDependsOnBuild=False `
			/v:Quiet `
			/noLogo `
			/clp:ErrorsOnly
			
		# Copie dans le répertoire de livraison
		$packageBuildFolder = $projectPath | split-path -parent
		if($configuration -eq "ReleaseFoundation")
		{
			$packageBuildFolder += "\bin\Release Foundation"
		}
		else
		{
			$packageBuildFolder += "\bin\" + $configuration
		}
		
		Get-ChildItem $packageBuildFolder -Filter "*.wsp" | Move-Item -Destination $outputPath -Force -Confirm:$false
	}
	else
	{
		# Compilation
		#Write-Host "`tBuilding ..." 
		. C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe  `
			/p:Configuration=$configuration `
			/m $projectPath `
			/t:Rebuild `
			/p:UseWPP_CopyWebApplication=True `
			/p:PipelineDependsOnBuild=False `
			/v:Quiet `
			/noLogo `
			/clp:ErrorsOnly
	}
	
	$timer.Stop()
	$duration = [System.String]::Format("{0:00}'{1:00}`"", $timer.Elapsed.Minutes, $timer.Elapsed.Seconds)
	
	Write-Host " Done in $duration" -f Green
}

function Scan-VSSolution([string]$slnPath, [string]$slnFilename, [string]$config)
{
	$slnFilepath = $slnPath + "\" + $slnFilename
	
	$slnData = Get-Content $slnFilepath
	
	$result = @()
	
	# Création de la liste des projets à compiler eventuellement packager
	foreach($line in $slnData)
	{
		if($line.StartsWith("Project"))
		{
			$arr = $line.Split("`"")
			$csProjName = $arr[5]
			if($csProjName.EndsWith(".csproj"))
			{
				$csProjPath = $slnPath + "\" + $csProjName
				#Write-Host $csProjName
				
				# Est ce que c'est un projet SharePoint
				$isSharePointProj = $false
				$buffer = [xml](Get-Content $csProjPath -Encoding UTF8)
				$spTargets = $buffer.Project.Import | ?{$_.Project.ToString().EndsWith("Microsoft.VisualStudio.SharePoint.targets")}
				if($spTargets -ne $null)
				{
					$isSharePointProj = $true
				}
				
				$result += @{Path=$csProjPath;Name=$csProjName;Package=$isSharePointProj;csproj=($csProjName | Split-Path -Leaf);Config=$config}
			}
		}
	}
	
	# Retour de fonction
	return $result
}

function Build-AllProjects([System.Object[]]$htAllProjets)
{
	# Compilation de l'ensemble des projets
	$exclude = @()
	Write-Host "Compilation de l'ensemble des projets" -ForegroundColor Yellow
	$htAllProjets | %{
		if($exclude -notcontains $_.csproj)
		{
			Write-Host "`tBuilding " ($_.Name | Split-Path -Leaf) -NoNewLine
			Build-Package $_.Config $_.Path $outputPath $false
			$exclude += $_.csproj
		}
	}

	# Package des projets de type WSP
	$exclude = @()
	Write-Host "Packaging de l'ensemble des projets de type WSP" -ForegroundColor Yellow
	$htAllProjets | ?{$_.Package -eq $true} | %{
		if($exclude -notcontains $_.csproj)
		{
			Write-Host "`tPackaging " ($_.Name | Split-Path -Leaf) -NoNewLine
			Build-Package $_.Config $_.Path $outputPath $true
			$exclude += $_.csproj
		}
	}
}

#------------------------------------------------------------------------------------------------------------
# Variables de script
#------------------------------------------------------------------------------------------------------------
$currentScriptPath = $MyInvocation.MyCommand.Path | Split-Path -Parent
$outputPath = $currentScriptPath + "\<<define output path>>\"
$allProjects = @()

#------------------------------------------------------------------------------------------------------------
# Traitement
#------------------------------------------------------------------------------------------------------------

$globalTimer = [System.Diagnostics.StopWatch]::StartNew()

#Scan
$currentSlnPath = $currentScriptPath
$slnFilename = "MaSolution.sln"
$buildConfigName = "Release"
Write-Host "Scan de la solution $slnFilename" -ForegroundColor DarkRed
$ht = Scan-VSSolution $currentSlnPath $slnFilename $buildConfigName
$ht | %{
	$allProjects += $_
}

# Compil et Package
Write-Host "Compilation et Packaging des projets" -ForegroundColor DarkRed
Build-AllProjects $allProjects

# FeedBack
$globalTimer.Stop()
$globalDuration = [System.String]::Format("{0:00}'{1:00}`"", $globalTimer.Elapsed.Minutes, $globalTimer.Elapsed.Seconds)

Write-Host ">> Job Done in $globalDuration" -f Green

#------------------------------------------------------------------------------------------------------------